//
//  XMMyOrderView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/26.
//

#import "XMMyOrderView.h"
#import "XMMyOrderTopTabView.h"
#import "XMMyOrderCell.h"

@interface XMMyOrderView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) XMMyOrderTopTabView *topTabView;
@property (nonatomic, strong) XMMyOrderTopTabView *orderTabView;

@property (nonatomic, strong) NSMutableArray *orderList;

@property (nonatomic, copy) NSString *buyOrShow;

@end

@implementation XMMyOrderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navView.backgroundColor = kColorHEX(0xffffff);
    [self.navView addSubview:self.topTabView];
    [self addSubview:self.orderTabView];
    [self addSubview:self.tbl];
}

- (void)setContentWithOrderList:(NSArray *)orderList
                      buyOrShow:(nonnull NSString *)buyOrShow
                           page:(NSInteger)page {
    _buyOrShow = buyOrShow;
    [_tbl.mj_header endRefreshing];
    if (page == 1) {
        [self.orderList removeAllObjects];
    }
    if (orderList.count < 10) {
        [_tbl.mj_footer endRefreshingWithNoMoreData];
    }
    else {
        [_tbl.mj_footer endRefreshing];
    }
    [self.orderList addObjectsFromArray:orderList];
    [_tbl reloadData];
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.orderList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(8.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.width, KScaleWidth(8.0))];
    headView.backgroundColor = kColorHEX(0xF0F0F0);
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(310.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMMyOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMMyOrderCellName"];
    XMMyOrderModel *orderModel = self.orderList[indexPath.section];
    [cell setContentWithOrderModel:orderModel buyOrShow:_buyOrShow];
    XMWeakSelf;
    cell.posiBtnActionBlock = ^(NSInteger posiType) {
        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(funcActionWithPosiType:orderModel:)]) {
            [weakSelf.delegate funcActionWithPosiType:posiType orderModel:@[orderModel]];
        }
//        if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(funcActionWithPosiType:orderStatus:orderId:orderOrgId:serviceId:refundId:price:)]) {
//            [weakSelf.delegate funcActionWithPosiType:posiType
//                                          orderStatus:orderModel.status orderId:orderModel.orderId
//                                           orderOrgId:orderModel.orderOrgId
//                                            serviceId:orderModel.serviceId
//                                             refundId:orderModel.refundId
//                                                price:orderModel.price];
//        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XMMyOrderModel *orderModel = self.orderList[indexPath.section];
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatOrderDetailWithOrderId:orderStatus:)]) {
        [self.delegate chatOrderDetailWithOrderId:orderModel.orderId
                                      orderStatus:orderModel.status];
    }
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH + KScaleWidth(55.0), self.width, self.height - kNavagationBarH - KScaleWidth(55.0)) style:UITableViewStylePlain];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        [_tbl registerClass:[XMMyOrderCell class] forCellReuseIdentifier:@"XMMyOrderCellName"];
        _tbl.backgroundColor = kColorHEX(0xF0F0F0);
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
        XMWeakSelf;
        _tbl.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(refreshData)]) {
                [weakSelf.delegate refreshData];
            }
        }];
        _tbl.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loadMore)]) {
                [weakSelf.delegate loadMore];
            }
        }];
    }
    return _tbl;
}

#pragma mark - Init

- (XMMyOrderTopTabView *)topTabView {
    if (!_topTabView) {
        _topTabView = [[XMMyOrderTopTabView alloc] initWithFrame:CGRectMake(44, kNavagationBarH - 44, self.width - 88, 44) titleArr:@[@"我买到的", @"我售出的"] space:KScaleWidth(75.0) lineColor:kColorHEX(0xffffff) lineWidth:0 fontSize:KScaleWidth(18.0) titleColor:kColorHEX(0x666666) selectColor:kColorHEX(0x333333)];
        XMWeakSelf;
        _topTabView.itemSelectBlock = ^(NSInteger itemSelect) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(topTabSelect:)]) {
                [weakSelf.delegate topTabSelect:@[@"1", @"2"][itemSelect]];
            }
        };
    }
    return _topTabView;
}

- (XMMyOrderTopTabView *)orderTabView {
    if (!_orderTabView) {
        _orderTabView = [[XMMyOrderTopTabView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, KScaleWidth(55.0)) titleArr:@[@"全部", @"待付款", @"待见面", @"见面中", @"待评价", @"退款"] space:KScaleWidth(4.0) lineColor:kColorHEX(0xFFD800) lineWidth:KScaleWidth(25.0) fontSize:KScaleWidth(15.0) titleColor:kColorHEX(0xBDBDBD) selectColor:kColorHEX(0x333333)];
        XMWeakSelf;
        _orderTabView.itemSelectBlock = ^(NSInteger itemSelect) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(statusSelectIndex:)]) {
                [weakSelf.delegate statusSelectIndex:[NSString stringWithFormat:@"%ld", itemSelect*10]];
            }
        };
    }
    return _orderTabView;
}

- (NSMutableArray *)orderList {
    if (!_orderList) {
        _orderList = [NSMutableArray array];
    }
    return _orderList;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
